home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUTOR.ZIP / LESSON1 < prev    next >
Text File  |  1995-02-15  |  21KB  |  372 lines

  1. Lesson 1-Intel 8088, 80186, 80286,80386, 80486 Assembly Language Programming:
  2. ----------------------------------------------------------------------------
  3. OVERVIEW:
  4.  
  5. The Intel  microprocessors above plus the Pentium are all upward compatible.
  6. That is, a program written for the 8088 will run any processor above it.  We
  7. shall start off by studying the first one in the family above, the 8088 and
  8. progress later to the 80286.
  9.  
  10. The heart of a microcomputer is the microprocessor called the 'central
  11. processing unit,' the CPU.  A microcomputer block diagram is below.
  12.  
  13.         ┌──────┐ 
  14.         │      │<--------- address bus ---------------
  15.         │      │               |                     |
  16.         │      │               |                     |
  17.         │ 8088 │               v                     v      keyboard in
  18.         │      │      ----- memory -----        --- I/O --- video out
  19.         │ CPU  │      ^                ^        ^         ^ port I/Os
  20.         │      │      |                |        |         |
  21.         │      │      v                |        v         |
  22.         │      │<------------------ data bus ------------>|
  23.         │      │                       |                  |
  24.         │      │                       v                  |
  25.         │      │<----------------- control bus ------------
  26.         └──────┘
  27.  
  28. The 8088 has 14 general registers of 16 bits each as illustrated below. 
  29. Eight bits = a byte and 2 bytes = a word.  Each of the first four 16 bit
  30. registers are divided into eight 8 bit registers also. Case may be ignored.
  31. Count zero as 1.
  32.                DATA REGISTERS
  33.                15      8 7       0
  34.             AX |---ah---|---al---|                        
  35.             BX |---bh---|---bl---| (BX also is an index register)
  36.             CX |---ch---|---cl---|
  37.             DX |---dh---|---dl---|
  38.  
  39.                POINTER & INDEX REGISTERS
  40.                15               0
  41.             SP |----------------| stack pointer
  42.             BP |----------------| base pointer
  43.             SI |----------------| source index
  44.             DI |----------------| destination index
  45.  
  46.                SEGMENT REGISTERS
  47.                15               0
  48.             CS |----------------| code segment
  49.             DS |----------------| data segment
  50.             SS |----------------| stack segment
  51.             ES |----------------| extra segment
  52.  
  53.                INSTRUCTION POINTER & FLAGS
  54.                15               0
  55.             IP |----------------| instruction point
  56.             F  |----ODITSZ-A-P-C| flags register
  57.  
  58. Please read pages 1, 2 and 3 of the iAPX 8088 User Manual I am sent you.
  59.  
  60. Let us skip theory for a while and write and assemble and run a real working
  61. assembly language program.
  62.  
  63. We will write our program using an EDITOR.  We will use EDLIN.COM modified to 
  64. run on any version of the DOS operating system since it has line numbers that
  65. make it easier for us to refer to it.  What it creates is called SOURCE code.
  66.  
  67. We will assemble our source code using the A86 assembler.  It translates the
  68. source code into machine code the the CPU understands. It is the program that
  69. we will RUN and it is called a .COM file.  If our source code were named
  70. DEMO1.ASM, after assembly it would be named DEMO1.COM, the program to run.
  71.  
  72. Our first program will be named DEMO1.ASM which we will create using 
  73. EDLIN.COM.  Type EDLIN DEMO1.ASM and press enter.  Then press I1 (insert line
  74. 1) and press enter.  Now type in the following lines.  If you make a mistake,
  75. use the backspace key to backup and erase.  Press enter when done with each
  76. line.  To escape from input press Ctrl C.  Then press L to list, or I line 
  77. number to Insert a new line, or press P line number to display 23 lines
  78. starting with the line number.  Ctrl C will exit off a line number and await
  79. a command.  Then typing in the line number and pressing enter will take you
  80. to it for further edit.  the Insert key will insert until it is pressed
  81. again.  The Delete key will delete the character above the cursor.  When all
  82. done, press Ctrl C and enter, then E for end.  You are all done!
  83.  
  84. Read the DOS 5.0 User's Manual for more EDLIN functions.
  85.  
  86. DEMO1.ASM:
  87.  
  88.  1:     mov     ax,3                            ;rest video to text mode
  89.  2:     int     10h                             ;= clear the screen (CLS)
  90.  3:     mov     es,0b800h                       ;set es to text video segment
  91.  4:     mov     si,message                      ;message to display
  92.  5:     mov     di,1760                         ;display at line 11 on video
  93.  6:     mov     ah,7                            ;medium white color
  94.  7:nex1: mov    al,cs:[si]                      ;byte from code seg message
  95.  8:     inc     si                              ;message +1
  96.  9:     cmp     al,0                            ;end of message ?
  97. 10:     jz      done                            ;if so, jump to done
  98. 11:     mov     es:[di],ax   ;ax is a 2 byte word - move byte+color to video
  99. 12:     add     di,2                            ;next video word address
  100. 13:     jmp     nex1                            ;do next video
  101. 14:done: mov    ah,0                            ;wait for any key pressed
  102. 15:     int     16h                             ;keyboard interrupt
  103. 16:     mov     ax,4c00h                        ;interrupt 21h exit instruct.
  104. 17:     int     21h                             ;return to DOS> ready prompt
  105. 18:message:
  106. 19:db 'Captain Russell is now in command of the Starship Enterprise.',0
  107.  
  108. Now we are ready to assemble the DEMO1.ASM source code.  Type: 
  109.  
  110.               A86 +LS DEMO1.ASM and press enter
  111.  
  112. Eureka!  In a fraction of a second the A86 assembler created DEMO1.COM.
  113.  
  114. To run it type DEMO1.COM and press enter. To return to the DOS> ready prompt, 
  115. press any key.
  116.  
  117. We have frivolously skipped over a great many fundamentals we will cover 
  118. later, but at least we have discussed the registers available and created a 
  119. real honest to goodness working assembly language program that required only 
  120. 19 lines of source code. A considerable accomplishment for our first program.
  121.  
  122. Wait a minute!  The cursor was still blinking when we ran DEMO1COM.  How may
  123. turn it off?  Ok, let us CALL the following routine that will turn it off
  124. or on depending whether we call curson or cursof.
  125.  
  126. A CALL means to push the instruction pointer on the stack, go to the called 
  127. name, do whatever it says and then ret (return to the next instruction after
  128. the call).  The instruction pointer always has the address of the next
  129. instruction in it.  
  130.  
  131.      curson: mov     ah,1               ;set cursor type
  132.          mov     cx,0607h           ;cursor normal type
  133.          int     10h                ;bios video interrupt
  134.          mov     dx,0               ;set video top left position
  135.          jmp     >c1                ;> = forward jump
  136.      cursof: mov     dx,1900h           ;cursor out of view on text page
  137.      c1:     mov     ah,2               ;set cursor position
  138.          mov     bh,0               ;page zero in text mode
  139.          int     10h                ;bios video interrupt
  140.          ret                        ;return to call + next instruction
  141.  
  142. Bios means the computer's Basic Input Output System code instructions that
  143. DOS (the Disk Operating System) loads whenever the computer is turned on.
  144.  
  145. Very good.  Now let us add the above code to our 19 line program.
  146.  
  147. DEMO2.ASM
  148.  
  149.  1:     mov     ax,3                            ;rest video to text mode
  150.  2:     int     10h                             ;= clear the screen (CLS)
  151.  3:     mov     es,0b800h                       ;set es to text video segment
  152.  4:     call    cursof                          ;turn off blinking cursor
  153.  5:     mov     si,message                      ;message to display
  154.  6:     mov     di,1760                         ;display at line 11 on video
  155.  7:     mov     ah,7                            ;medium white color
  156.  8:nex1: mov    al,cs:[si]                      ;byte from code seg message
  157.  9:     inc     si                              ;message +1
  158. 10:     cmp     al,0                            ;end of message ?
  159. 11:     jz      done                            ;if so, jump to done
  160. 12:     mov     es:[di],ax   ;ax is a 2 byte word - move byte+color to video
  161. 13:     add     di,2                            ;next video word address
  162. 14:     jmp     nex1                            ;do next video
  163. 15:done: mov    ah,0                            ;wait for any key pressed
  164. 16:     int     16h                             ;keyboard interrupt
  165. 17:     call    curson                          ;turn blinking cursor back on
  166. 18:     mov     ax,4c00h                        ;interrupt 21h exit instruct.
  167. 19:     int     21h                             ;return to DOS> ready prompt
  168. 20:message:
  169. 21:db 'Captain Russell is now in command of the Starship Enterprise.',0
  170. 22:curson: mov  ah,1                            ;set cursor type
  171. 23:     mov     cx,0607h                        ;cursor normal type
  172. 24:     int     10h                             ;bios video interrupt
  173. 25:     mov     dx,0                            ;set video top left  
  174. 26:     jmp     >c1                             ;> = forward jump
  175. 27:cursof: mov  dx,1900h                        ;cursor out of view
  176. 28:c1:  mov     ah,2                            ;set cursor position
  177. 29:     mov     bh,0                            ;page zero in text mode
  178. 30:     int     10h                             ;bios video interrupt
  179. 31:     ret                                     ;return to call + next
  180.  
  181. Before we display the message, line 4 above, tells the program to turn off
  182. the blinking cursor.  Then, right after line 16 waits for a key press, line
  183. 17 turns the blinking cursor back on.
  184.  
  185. Since we frivolously skipped over a great many fundamentals to get our first
  186. programs running, let us take a step back and define a few of them before we 
  187. go on.
  188.  
  189. .COM PROGRAMS:
  190. When a .COM program is first loaded it sets the CS, DS and ES segment 
  191. registers to the program's location in memory and the SP (stack pointer
  192. to the top end of that segment.  A segment is 65,536 bytes long and your
  193. computer may access any of the 16 segments.  A .COM program always resides
  194. in only a single segment which is usually more than enough for most purposes,
  195. but it may switch to and use ANY segment for data and video, or jump far to
  196. a distant segment and use its code there and when desired, jump back to its
  197. original segment.  More later when we include EDMOD.COM (edit/modify any all 
  198. of memory) and then return to our program as one of the options our program 
  199. offers.
  200.  
  201. INTERRUPTS:
  202. The first 1024 byte page of segment zero may contain up to 256 different
  203. 4 byte interrupt addresses.  These 4 bytes contain a 2 byte word with the  
  204. segment of the interrupt, plus a 2 byte word of the address of the interrupt
  205. within that segment. They are usually located in high memory above the ninth
  206. segment.  Interrupts perform many functions including keyboard input, video  
  207. output, printer output and many more too numerous to mention.  Interrupt
  208. numbers are usually given in hexadecimal rather than decimal.
  209.  
  210. HEXADECIMAL & BINARY:
  211. Hex is simply another way of counting that is more convenient than decimal 
  212. when using computers that all have 8 bit bytes and 16 bit words.  Here is an
  213. equivalent table illustrating decimal, hex and binary equivalents for a few
  214. decimal numbers between 1 and 65535.  The binary equivalents' right hand bit 
  215. is bit 0 and the lefthand bit = bit 15 of the 2 bytes (= word) shown.
  216.  
  217.     DECIMAL         HEXADECIMAL               BINARY
  218.       word             word              2nd byte 1st byte
  219.          0             0000h             00000000 00000000b
  220.          1             0001h             00000000 00000001b
  221.          2             0002h             00000000 00000010b        
  222.          3             0003h             00000000 00000011b
  223.          4             0004h             00000000 00000100b
  224.          5             0005h             00000000 00000101b
  225.          6             0006h             00000000 00000110b
  226.          7             0007h             00000000 00000111b
  227.          8             0008h             00000000 00001000b
  228.          9             0009h             00000000 00001001b
  229.         10             000ah             00000000 00001010b
  230.         11             000bh             00000000 00001011b
  231.         12             000ch             00000000 00001100b
  232.         13             000dh             00000000 00001101b
  233.         14             000eh             00000000 00001110b
  234.         15             000fh             00000000 00001111b
  235.         16             0010h             00000000 00010000b
  236.         32             0020h             00000000 00100000b
  237.         64             0040h             00000000 01000000b
  238.        128             0080h             00000000 10000000b
  239.        255             00ffh             00000000 11111111b
  240.        256             0100h             00000001 00000000b
  241.      65535             ffffh             11111111 11111111b
  242.      65536            10000h           1 00000000 00000000b
  243.  
  244. Here is an easy way to convert from decimal to hex and hex to decimal using 
  245. the little table that follows. 
  246.      -------------------------------------------------------------------
  247.      | HEX       DEC  | HEX      DEC  | HEX       DEC  | HEX       DEC |
  248.      -------------------------------------------------------------------
  249.      |  0          0  |  0         0  |  0          0  |  0          0 |
  250.      |  1      4,096  |  1       256  |  1         16  |  1          1 |    
  251.      |  2      8,192  |  2       512  |  2         32  |  2          2 |
  252.      |  3     12,288  |  3       768  |  3         48  |  3          3 |
  253.      |  4     16,384  |  4     1,024  |  4         64  |  4          4 |
  254.      |  5     20,480  |  5     1,280  |  5         80  |  5          5 |
  255.      |  6     24,576  |  6     1,536  |  6         96  |  6          6 |
  256.      |  7     28,672  |  7     1,792  |  7        112  |  7          7 |
  257.      |  8     32,768  |  8     2,048  |  8        128  |  8          8 |
  258.      |  9     36,864  |  9     2,304  |  9        144  |  9          9 |
  259.      |  A     40,960  |  A     2,560  |  A        160  |  A         10 |
  260.      |  B     45,056  |  B     2,816  |  B        176  |  B         11 |
  261.      |  C     49,152  |  C     3,072  |  C        192  |  C         12 |
  262.      |  D     53,248  |  D     3,328  |  D        208  |  D         13 |
  263.      |  E     57,344  |  E     3,584  |  E        224  |  E         14 |
  264.      |  F     61,440  |  F     3,840  |  F        240  |  F         15 |
  265.      -------------------------------------------------------------------
  266. USING THE ABOVE TABLE:
  267. Assume we wish to convert 1111h to decimal.  Then take the 1's equivalents
  268. from left to right and add them up.  4096
  269.                     + 256
  270.                     +  16
  271.                     +   1
  272.                     -----
  273.                    = 4369 decimal
  274.  
  275. Converting binary numbers to hex is even easier.  Just take the binary number 
  276. and divide it into groups of 4 as below and you have got it:
  277.  
  278.           binary number = 1111 1111 1111 1111
  279.                 hex =  F    F    F    F
  280.  
  281. Enclosed is a printed out table of equivalents for decimal, binary, hex and
  282. ASCII (for IBM compatibles) that I keep handy on my desk in a clear plastic 
  283. cover for reference.  ASCII (American Standard Code For Information Inter-  
  284. change) is the character displayed on video when that byte value is loaded
  285. into video memory in text mode.  The values below 32 decimal are mainly 
  286. printer control codes so their character equivalent is not printed out.  I
  287. have inked in these characters on the printed out version mailed to you.
  288.  
  289. The table is illustrated below.
  290.  
  291.          IBM - ASCII - DECIMAL - BINARY - HEX - TABLE
  292.  
  293.    0 00000000 00H  @  64 01000000 40H  Ç 128 10000000 80H  └ 192 11000000 C0H
  294.    1 00000001 01H  A  65 01000001 41H  ü 129 10000001 81H  ┴ 193 11000001 C1H
  295.    2 00000010 02H  B  66 01000010 42H  é 130 10000010 82H  ┬ 194 11000010 C2H
  296.    3 00000011 03H  C  67 01000011 43H  â 131 10000011 83H  ├ 195 11000011 C3H
  297.    4 00000100 04H  D  68 01000100 44H  ä 132 10000100 84H  ─ 196 11000100 C4H
  298.    5 00000101 05H  E  69 01000101 45H  à 133 10000101 85H  ┼ 197 11000101 C5H
  299.    6 00000110 06H  F  70 01000110 46H  å 134 10000110 86H  ╞ 198 11000110 C6H
  300.    7 00000111 07H  G  71 01000111 47H  ç 135 10000111 87H  ╟ 199 11000111 C7H
  301.    8 00001000 08H  H  72 01001000 48H  ê 136 10001000 88H  ╚ 200 11001000 C8H
  302.    9 00001001 09H  I  73 01001001 49H  ë 137 10001001 89H  ╔ 201 11001001 C9H
  303.   10 00001010 0AH  J  74 01001010 4AH  è 138 10001010 8AH  ╩ 202 11001010 CAH
  304.   11 00001011 0BH  K  75 01001011 4BH  ï 139 10001011 8BH  ╦ 203 11001011 CBH
  305.   12 00001100 0CH  L  76 01001100 4CH  î 140 10001100 8CH  ╠ 204 11001100 CCH
  306.   13 00001101 0DH  M  77 01001101 4DH  ì 141 10001101 8DH  ═ 205 11001101 CDH
  307.   14 00001110 0EH  N  78 01001110 4EH  Ä 142 10001110 8EH  ╬ 206 11001110 CEH
  308.   15 00001111 0FH  O  79 01001111 4FH  Å 143 10001111 8FH  ╧ 207 11001111 CFH
  309.   16 00010000 10H  P  80 01010000 50H  É 144 10010000 90H  ╨ 208 11010000 D0H
  310.   17 00010001 11H  Q  81 01010001 51H  æ 145 10010001 91H  ╤ 209 11010001 D1H
  311.   18 00010010 12H  R  82 01010010 52H  Æ 146 10010010 92H  ╥ 210 11010010 D2H
  312.   19 00010011 13H  S  83 01010011 53H  ô 147 10010011 93H  ╙ 211 11010011 D3H
  313.   20 00010100 14H  T  84 01010100 54H  ö 148 10010100 94H  ╘ 212 11010100 D4H
  314.   21 00010101 15H  U  85 01010101 55H  ò 149 10010101 95H  ╒ 213 11010101 D5H
  315.   22 00010110 16H  V  86 01010110 56H  û 150 10010110 96H  ╓ 214 11010110 D6H
  316.   23 00010111 17H  W  87 01010111 57H  ù 151 10010111 97H  ╫ 215 11010111 D7H
  317.   24 00011000 18H  X  88 01011000 58H  ÿ 152 10011000 98H  ╪ 216 11011000 D8H
  318.   25 00011001 19H  Y  89 01011001 59H  Ö 153 10011001 99H  ┘ 217 11011001 D9H
  319.   26 00011010 1AH  Z  90 01011010 5AH  Ü 154 10011010 9AH  ┌ 218 11011010 DAH
  320.   27 00011011 1BH  [  91 01011011 5BH  ¢ 155 10011011 9BH  █ 219 11011011 DBH
  321.   28 00011100 1CH  \  92 01011100 5CH  £ 156 10011100 9CH  ▄ 220 11011100 DCH
  322.   29 00011101 1DH  ]  93 01011101 5DH  ¥ 157 10011101 9DH  ▌ 221 11011101 DDH
  323.   30 00011110 1EH  ^  94 01011110 5EH  ₧ 158 10011110 9EH  ▐ 222 11011110 DEH
  324.   31 00011111 1FH  _  95 01011111 5FH  ƒ 159 10011111 9FH  ▀ 223 11011111 DFH
  325.   32 00100000 20H  `  96 01100000 60H  á 160 10100000 A0H  α 224 11100000 E0H
  326. ! 33 00100001 21H  a  97 01100001 61H  í 161 10100001 A1H  ß 225 11100001 E1H
  327. " 34 00100010 22H  b  98 01100010 62H  ó 162 10100010 A2H  Γ 226 11100010 E2H
  328. # 35 00100011 23H  c  99 01100011 63H  ú 163 10100011 A3H  π 227 11100011 E3H
  329. $ 36 00100100 24H  d 100 01100100 64H  ñ 164 10100100 A4H  Σ 228 11100100 E4H
  330. % 37 00100101 25H  e 101 01100101 65H  Ñ 165 10100101 A5H  σ 229 11100101 E5H
  331. & 38 00100110 26H  f 102 01100110 66H  ª 166 10100110 A6H  µ 230 11100110 E6H
  332. ' 39 00100111 27H  g 103 01100111 67H  º 167 10100111 A7H  τ 231 11100111 E7H
  333. ( 40 00101000 28H  h 104 01101000 68H  ¿ 168 10101000 A8H  Φ 232 11101000 E8H
  334. ) 41 00101001 29H  i 105 01101001 69H  ⌐ 169 10101001 A9H  Θ 233 11101001 E9H
  335. * 42 00101010 2AH  j 106 01101010 6AH  ¬ 170 10101010 AAH  Ω 234 11101010 EAH
  336. + 43 00101011 2BH  k 107 01101011 6BH  ½ 171 10101011 ABH  δ 235 11101011 EBH
  337. , 44 00101100 2CH  l 108 01101100 6CH  ¼ 172 10101100 ACH  ∞ 236 11101100 ECH
  338. - 45 00101101 2DH  m 109 01101101 6DH  ¡ 173 10101101 ADH  φ 237 11101101 EDH
  339. . 46 00101110 2EH  n 110 01101110 6EH  « 174 10101110 AEH  ε 238 11101110 EEH
  340. / 47 00101111 2FH  o 111 01101111 6FH  » 175 10101111 AFH  ∩ 239 11101111 EFH
  341. 0 48 00110000 30H  p 112 01110000 70H  ░ 176 10110000 B0H  ≡ 240 11110000 F0H
  342. 1 49 00110001 31H  q 113 01110001 71H  ▒ 177 10110001 B1H  ± 241 11110001 F1H
  343. 2 50 00110010 32H  r 114 01110010 72H  ▓ 178 10110010 B2H  ≥ 242 11110010 F2H
  344. 3 51 00110011 33H  s 115 01110011 73H  │ 179 10110011 B3H  ≤ 243 11110011 F3H
  345. 4 52 00110100 34H  t 116 01110100 74H  ┤ 180 10110100 B4H  ⌠ 244 11110100 F4H
  346. 5 53 00110101 35H  u 117 01110101 75H  ╡ 181 10110101 B5H  ⌡ 245 11110101 F5H
  347. 6 54 00110110 36H  v 118 01110110 76H  ╢ 182 10110110 B6H  ÷ 246 11110110 F6H
  348. 7 55 00110111 37H  w 119 01110111 77H  ╖ 183 10110111 B7H  ≈ 247 11110111 F7H
  349. 8 56 00111000 38H  x 120 01111000 78H  ╕ 184 10111000 B8H  ° 248 11111000 F8H
  350. 9 57 00111001 39H  y 121 01111001 79H  ╣ 185 10111001 B9H  ∙ 249 11111001 F9H
  351. : 58 00111010 3AH  z 122 01111010 7AH  ║ 186 10111010 BAH  · 250 11111010 FAH
  352. ; 59 00111011 3BH  { 123 01111011 7BH  ╗ 187 10111011 BBH  √ 251 11111011 FBH
  353. < 60 00111100 3CH  | 124 01111100 7CH  ╝ 188 10111100 BCH  ⁿ 252 11111100 FCH
  354. = 61 00111101 3DH  } 125 01111101 7DH  ╜ 189 10111101 BDH  ² 253 11111101 FDH
  355. > 62 00111110 3EH  ~ 126 01111110 7EH  ╛ 190 10111110 BEH  ■ 254 11111110 FEH
  356. ? 63 00111111 3FH   127 01111111 7FH  ┐ 191 10111111 BFH    255 11111111 FFH 
  357.  
  358. We sure have covered a lot of territory in this first week's curriculum.  It
  359. should give you a slight inkling of what assembly language is all about.
  360.  
  361. Since our classrooms are about 1500 miles apart, I have included about 30
  362. lines at the end of the quiz for you to ask questions.  Feel free to add as
  363. many more questions as you wish.  I will e-mail you the answers to your
  364. questions the day after I receive them.
  365.  
  366. You now have the Intel microprocessor 8088 User Manual I have ordered for 
  367. you.
  368.  
  369. Love, Grandpa
  370.  
  371.  
  372.